/*****Orbit Script*****/
//Contains data and utility functions pertaining to the orbits

//Global control variables for handling the qualities of orbits
var ORBITAL_SPEED = 12000; //Speed constant modifying all orbits in system
var ORBITAL_CHANGE_SPEED = 5; //Determines how quickly the ship can change orbit
var ORBIT_COUNT = 31; //Number of orbits
var ORBIT_DISTANCE = 100; //Distance in pixels one orbit is from another
var ORBIT_OFFSET = 100; //Distance between 0th orbit and planet
var ORBIT_SNAP_THRESHOLD = 2; //Determines how close the ship must come to an
							  //orbit before it snaps to it.
							  
var CLOCKWISE = 1;
var COUNTERCLOCKWISE = -1;
//Variables pertaining to the planet object itself. May be separate module in
//the future.
var PLANET_XPOS = 0.0;
var PLANET_YPOS = 0.0;
var PLANET_RADIUS = 500;


//Array to store various radii of orbits
var orbitPositions = [];

//could be used for difficulty level
var orbitalSpeed = ORBITAL_SPEED;
var orbitalChangeSpeed = ORBITAL_CHANGE_SPEED;


//Gets the current distance from the planet of the given
//orbit
function Orbit_getDistance(line){
	return orbitPositions[line];
}

//Initializes the orbits and places them in orbitPositions[]
//Lower-numbered orbits are closer to the planet.
function Orbit_createOrbits(){
	orbitPositions = [];
	for(var i = 0; i < ORBIT_COUNT; i++){
		orbitPositions.push(ORBIT_OFFSET + (i*ORBIT_DISTANCE) + PLANET_RADIUS);
	}
}


//Draws all orbits around the planet
function Orbit_drawOrbits(){
	for(var i = 0; i < ORBIT_COUNT; i++){
		ctx.beginPath();
		ctx.lineWidth="2";
		ctx.strokeStyle="blue";
		ctx.arc(PLANET_XPOS, PLANET_YPOS, orbitPositions[i], 0, 2*Math.PI);
		ctx.stroke();
	}
}

//Gets the angular velocity of an object at a given position relative to the
//the planet, going in a given direction ((either positive or negative).
//In this case, velocity is inversely proportional to the 3/2 power of
//The distance from the planet. Positive angular velocities result in clockwise
//movement, while negative angular velocities result in counterclockwise movement.
function Orbit_getAngVelFree(xpos, ypos, direction){
	var height = Orbit_getPlanetDistance(xpos, ypos);
	var retval = Math.sqrt(orbitalSpeed/Math.pow(height, 3));
	if(direction > 0){
		return retval;
	}else if(direction < 0){
		return retval*(-1);
	}else{
		console.log("WARNING: directional value of zero detected in Orbit_getAngVel().");
	}
}

//Gets the angular velocity of an object occupying a given orbital position, orbit,
//going in a given direction In this case, velocity is inversely proportional to the
//square root of he distance from the planet. Positive angular velocities result in clockwise
//movement, while negative angular velocities result in counterclockwise movement.
function Orbit_getAngVelClosed(orbit, direction){
	var retval = Math.sqrt(orbitalSpeed/Math.pow(orbitPositions[orbit], 3));
	if(direction > 0){
		return retval;
	}else if(direction < 0){
		return retval*(-1);
	}else{
		console.log("WARNING: directional value of zero detected in Orbit_getAngVel().");
	}
}

//Returns the current x and y position of an entity in a given orbit with a given angle.
//An angle of zero denotes an object directly above the planet
function Orbit_getPos(orbit, angle){
	var xpos = Math.sin(angle)*(Orbit_getDistance(orbit));
	var ypos = Math.cos(angle-Math.PI)*(Orbit_getDistance(orbit));
	return {x:xpos, y:ypos};
}

function Orbit_getPosFree(distance, angle){
	var xpos = Math.sin(angle)*distance;
	var ypos = Math.cos(angle-Math.PI)*distance;
	return {x:xpos, y:ypos};
}

//Returns the current distance the given point in space is
//From the planet
function Orbit_getPlanetDistance(xpos, ypos){
	return Math.sqrt(Math.pow(xpos - PLANET_XPOS, 2) + Math.pow(ypos - PLANET_YPOS, 2));
}

//Returns a unit vector representing the direction from a given point (x,y)
//To the planet
function Orbit_getPlanetVector(x, y){
	var distance = Orbit_getPlanetDistance(x, y);
	var xComp = (x - PLANET_XPOS)/distance;
	var yComp = (y - PLANET_YPOS)/distance;
	return{x:xComp, y:yComp};
}

//Converts a raw pixel length on a given orbit into an
//angular value that other systems can use. Lengths are calculated
//running along the path of the orbit.
function Orbit_pixelToAngle(orbit, length){
	return (length/orbitPositions[orbit]);
}

//Converts an angular length on a given orbit into a
//raw pixel value. Lengths are calculated running along the
//path of the orbit.
function Orbit_angleToPixel(orbit, angle){
	return (angle*orbitPositions[orbit]);
}
